package common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置文件解析
 * 
 * @author fudl
 * @date 2011.04.08
 */
public class PropertiesTools {
	
	/**
	 * 根据properties名称获取Properties对象
	 * @param propertiesFileName
	 * @return
	 */
	public static Properties getPropertiesByFileName(String propertiesFileName) {
		InputStream is = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream(propertiesFileName);
		Properties p = new Properties();
		try {
			p.load(is);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return p;
	}

	/**
	 * 获得配置文件信息
	 * 
	 * @param configFile
	 *            配置文件路径
	 * @param key
	 *            配置文件的KEY
	 * @return 配置文件的值
	 */
	public static String getProperties(String configFile, String key) {
		InputStream inputStream = PropertiesTools.class.getClassLoader()
				.getResourceAsStream(configFile);
		Properties p = new Properties();
		String value = "";
		try {
			p.load(inputStream);
			value = p.getProperty(key);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return value;
	}
	
	public static void main(String[] args) {
		String s = PropertiesTools.getProperties("SgipProperties.properties","host");
		System.out.println(s);
	}

}
